package com.jevoly.service.impl;

import com.jevoly.service.FileService;
import com.jevoly.utils.FileUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.Arrays;
import java.util.List;

@Service
public class FileServiceImpl implements FileService {

    @Value("${params.serverAlbum}")
    String serverAlbum;

    @Value("${params.allowSuffix}")
    String strSuffix;

    @Override
    public List<String> getImageNameList() {
        List<String> ret = null;
        String[] suffixes = strSuffix.split(",");
        if (suffixes.length == 1) {
            ret = FileUtil.getFileName(getAlbumPath(), strSuffix, false);
        } else {
            ret = FileUtil.getFileName(getAlbumPath(), Arrays.asList(suffixes), false);
        }
        return ret;
    }

    @Override
    public String getImageNamesString() {
        return FileUtil.listToString(getImageNameList(), ",".charAt(0)) + ":" + serverAlbum;
    }

    @Override
    public List<String> getImageList() {
        List<String> ret = FileUtil.getFileName(getAlbumPath(), strSuffix);
        return ret;
    }

    @Override
    public String getAlbumPath() {
        File file = new File(FileUtil.getStaticUrl() + serverAlbum);
        return file.getPath();
    }

    @Override
    public String buildReturnImageString(String imgName) {
        return "img:" + imgName + ":" + serverAlbum;
    }

    @Override
    public void copyFilesToDirectory(String source, String destination) {
        /*
            1 删除目标目录中的文件
            2 将源目录中的文件拷贝到目标目录中
         */
        FileUtil.deleteDir(destination);
        FileUtil.dirCopy(source, destination);
    }
}
 